(function () {
	console.log('chrome_extension.js injected');
	function signal(data) {
		try {
			window.postMessage({
				signature: 'ushout',
				content: data
			}, "*");
		} catch (e) {}
	}
	
	// hack pushState to send a signal to us
	var _pushState = history.pushState;
	history.pushState = function() {
		signal({
			name: 'pushState',
			val: arguments
		});
		return _pushState.apply(this, arguments);
	};
	
	// youtube API will call this function when the video player is ready
	window.onYouTubePlayerReady = function (playerId) {
		signal({
			name: 'onYouTubePlayerReady',
			val: arguments
		});
	};
	
	window.ushout_playerOnStateChange = function (newState) {
		signal({
			name: 'playerOnStateChange',
			val: arguments
		});
	};
	
	window.ushout_playerOnPlaybackQualityChange = function (newQuality) {
		signal({
			name: 'playerOnPlaybackQualityChange',
			val: arguments
		});
	};
	
	window.ushout_playerOnPlaybackRateChange = function (newRate) {
		signal({
			name: 'playerOnPlaybackRateChange',
			val: arguments
		});
	};
	
	window.ushout_playerOnError = function (error) {
		signal({
			name: 'playerOnError',
			val: arguments
		});
	};
	
	window.ushout_playerOnApiChange = function () {
		signal({
			name: 'playerOnApiChange',
			val: arguments
		});
	};
})(window, document);


